CC = gcc
CFLAGS = -Wall -Wextra -g -O2 -std=c99
LDFLAGS = -lm

SRC_DIR = src
INC_DIR = include
OBJ_DIR = obj

# Source files
SRC = $(wildcard $(SRC_DIR)/*.c)
OBJ = $(patsubst $(SRC_DIR)/%.c, $(OBJ_DIR)/%.o, $(SRC))

# Main target
TARGET = curve_fitting

# Create directories
$(shell mkdir -p $(OBJ_DIR))

# Default target
all: $(TARGET)

# Link object files to create executable
$(TARGET): $(OBJ)
	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)

# Compile source files to object files
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
	$(CC) $(CFLAGS) -I$(INC_DIR) -c -o $@ $<

# Clean up
clean:
	rm -rf $(OBJ_DIR) $(TARGET)

# Run tests
test: $(TARGET)
	./$(TARGET) gendat test.dat 1 2.5 -3.0
	./$(TARGET) fitting test.dat ls 1
	./$(TARGET) gendat test.dat 2 1.0 -2.0 3.0
	./$(TARGET) fitting test.dat ls 2
	./$(TARGET) gendat test.dat 3 0.5 -1.0 2.0 -3.0
	./$(TARGET) fitting test.dat ls 3
	./$(TARGET) fitting test.dat gd 3

.PHONY: all clean test